package com.lpy.book.JFrame;

import com.lpy.book.dao.UserDao;
import com.lpy.book.model.DbUtil;
import com.lpy.book.model.User;
import com.lpy.book.utils.ToolUtil;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;

public class AdminUserInfo {
    JFrame jf;
    JTextField textField;
    JTextField textField_1;
    JTextField textField_2;
    JTextField textField_3;
    JTextField textField_4;
    JTextField textField_5;
    JButton btnNewButton_1;


    public AdminUserInfo() {
        jf = new JFrame();
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setTitle("111");
        jf.setSize(600, 800);
        jf.setLocation(400, 200);
        jf.setVisible(true);
        JPanel panel_1 = new JPanel();
        panel_1.setLayout(null);
        panel_1.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "\u7528\u6237\u4FE1\u606F", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(255, 0, 0)));
        panel_1.setBounds(20, 94, 541, 195);
        //做一个表头栏数据  一维数组
        String[] title = {"编号", "用户名", "密码", "性别", "电话"};
        /*具体的各栏行记录 先用空的二维数组占位*/
        String[][] dates = {};
        /*然后实例化 上面2个控件对象*/
        DefaultTableModel model;
        model = new DefaultTableModel(dates, title);
        JTable table = new JTable(model);
        putDates(new User());//获取数据库数据放置table中
        panel_1.setLayout(null);
        JScrollPane jscrollpane = new JScrollPane();
        jscrollpane.setBounds(20, 22, 496, 154);
        jscrollpane.setViewportView(table);
        panel_1.add(jscrollpane);
        jf.getContentPane().add(panel_1);
        jf.getContentPane().add(panel_1);
        JPanel panel_2 = new JPanel();
        panel_2.setBorder(new TitledBorder(null, "\u7528\u6237\u7F16\u8F91", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(255, 0, 0)));
        panel_2.setBounds(20, 302, 540, 137);
        jf.getContentPane().add(panel_2);
        panel_2.setLayout(null);
        JLabel lblNewLabel_1 = new JLabel("编号：");
        lblNewLabel_1.setFont(new Font("幼圆", Font.BOLD, 15));
        lblNewLabel_1.setBounds(49, 30, 48, 34);
        panel_2.add(lblNewLabel_1);
        textField_1 = new JTextField();
        textField_1.setEditable(false);
        textField_1.setBounds(103, 37, 66, 21);
        panel_2.add(textField_1);
        textField_1.setColumns(10);
        JLabel label = new JLabel("用户名：");
        label.setFont(new Font("幼圆", Font.BOLD, 15));
        label.setBounds(187, 30, 66, 34);
        panel_2.add(label);
        textField_2 = new JTextField();
        textField_2.setColumns(10);
        textField_2.setBounds(259, 37, 93, 21);
        panel_2.add(textField_2);
        JLabel label_1 = new JLabel("密码：");
        label_1.setFont(new Font("幼圆", Font.BOLD, 15));
        label_1.setBounds(383, 30, 48, 34);
        panel_2.add(label_1);
        textField_3 = new JTextField();
        textField_3.setColumns(10);
        textField_3.setBounds(437, 37, 93, 21);
        panel_2.add(textField_3);
        btnNewButton_1.setFont(new Font("幼圆", Font.BOLD, 15));
        btnNewButton_1.setBounds(422, 74, 87, 34);
        panel_2.add(btnNewButton_1);
        JLabel label_2 = new JLabel("性别：");
        label_2.setFont(new Font("幼圆", Font.BOLD, 15));
        label_2.setBounds(49, 74, 48, 34);
        panel_2.add(label_2);
        textField_4 = new JTextField();
        textField_4.setColumns(10);
        textField_4.setBounds(103, 81, 66, 21);
        panel_2.add(textField_4);
        JLabel label_3 = new JLabel("手机号：");
        label_3.setFont(new Font("幼圆", Font.BOLD, 15));
        label_3.setBounds(187, 74, 66, 34);
        panel_2.add(label_3);
        btnNewButton_1 = new JButton("修改");

        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String userId = textField_1.getText();
                String userName = textField_2.getText();
                String password = textField_3.getText();
                String sex = textField_4.getText();
                String phone = textField_5.getText();
                if (ToolUtil.isEmpty(userName) ||
                        ToolUtil.isEmpty(password) || ToolUtil.isEmpty(sex) ||
                        ToolUtil.isEmpty(phone)) {
                    JOptionPane.showMessageDialog(null, "请输入相关信息");
                    return;
                }
                User user = new User();
                user.setUserId(Integer.parseInt(userId));
                user.setUserName(userName);
                user.setPassword(password);
                user.setSex(sex);
                user.setPhone(phone);
                Connection con = null;
                try {
                    con = DbUtil.getConnection();
                    int i = UserDao.update(con, user);
                    if (i == 1) {
                        JOptionPane.showMessageDialog(null, "修改成功");
                        putDates(new User());
                    } else {
                        JOptionPane.showMessageDialog(null, "修改失败");
                    }
                } catch (Exception e1) {
                    e1.printStackTrace();
                    JOptionPane.showMessageDialog(null, "修改异常");
                } finally {
                    try {
                        DbUtil.close((com.mysql.jdbc.Connection) con, null, null);
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                }
            }


        });

    }

    private void putDates(User user) {

    }

    public static void main(String[] args) {
        AdminUserInfo adminUserInfo = new AdminUserInfo();
    }

}
